﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Enterprise.Model.OfficeDocument;
using Enterprise.Service.OfficeDocument;
using Enterprise.Infrastructure;
using Enterprise.Service.User;
using Enterprise.Control;
namespace Enterprise.UI.Web.Modules.OfficeDocument
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            OnCommand();
            OfficeDocService oService = new OfficeDocService();
            //所有发布的公文
            BindPublicGrid(oService);
            //我起草的公文
            BindMyGrid(oService);
            //待发布的公文
            BindUnPublicGrid(oService);

            if (UserService.GetUserDeptId(Utility.Get_UserId) != 2)
            {
                dPublic.Visible = false;
            }
        }

        private void OnCommand()
        {
            CreateBT.AddButton("new.gif", "起草公文", string.Format("Manage.aspx?Cmd=New"), Utility.PopedomType.New, HeadMenu1);
            CreateBT.AddButton("new.gif", "公文流转", string.Format("Manage.aspx?Cmd=New&typeId=1"), Utility.PopedomType.New, HeadMenu1);            
        }

        private void BindPublicGrid(OfficeDocService oService)
        {
            var list =oService.OfficeDocQianshouList( oService.OfficeDocumentList().Where(p => p.ODISPUBLIC == 1).ToList()).OrderByDescending(p => p.ODCREATETIME).ToList();
            GridView1.DataSource = list;
            GridView1.DataBind();           
        }

        private void BindMyGrid(OfficeDocService oService)
        {
            var mylist = oService.OfficeDocumentList().Where(p => p.ODUSERID == Utility.Get_UserId).OrderByDescending(p => p.ODCREATETIME);
            GridView2.DataSource = mylist;
            GridView2.DataBind();
        }

        public void BindUnPublicGrid(OfficeDocService oService)
        {
            var unpublic = oService.OfficeDocumentList().Where(p => p.ODSTATE==1&&p.ODISPUBLIC==0).OrderByDescending(p => p.ODCREATETIME);
            GridView3.DataSource = unpublic;
            GridView3.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            OfficeDocService oService = new OfficeDocService();
            BindPublicGrid(oService);
        }


        /// <summary>
        /// 取得公文发布状态
        /// </summary>
        /// <param name="odId">公文编号</param>
        /// <param name="odState">状态</param>
        /// <param name="odPublic">是否发布</param>
        /// <returns></returns>
        protected string GetPublicState(string odId, int odState,int odPublic,int odCreateUserId)
        {
            string str = "";
            if (odState == 1)
            {
                if (odPublic == 1)
                {
                    str = "已发布";
                }
                else
                {
                    str = "未发布";
                }
            }
            return str;
        }

        protected void Bt_Search_OnClick(object sender, EventArgs e)
        {

        }

    }
}